<?php
namespace app\index\home;
use app\common\controller\Common;
use think\Db;

class Index extends Common
{
    /**
     * 登录页面
     * @return string
     */
    public function index()
    {  
        if(session('index_daili.id')){
            $daili_id = session('index_daili.id');
            $daili_info = Db::name('daili')->where("id = '{$daili_id}'")->find();
            
            if($daili_info['wxtx'] == ''){
                $daili_info['wxtx'] = $daili_info['ysz'];
            }
        }else{
            $daili_info['wxtx'] = '';
            $daili_info['dlname'] = '';
        }
        
        $this->assign("daili_info",$daili_info);
        return $this->fetch();
    }
    
    /**
     * 登录系统
     */
    public function login(){
        $data = $this->request->post();
        $daili_info = Db::name('daili')->where("dl_phone = '{$data['username']}'")->find();
        if(empty($daili_info)){
            $this->error('账号不存在！');
        }
        if($daili_info['sh_status'] == 0){
            $this->error('您的账号还在审核阶段！');
        }
        if($daili_info['status'] == 0){
            $this->error('您的账号被锁定了！');
        }
        if(md5($data['password']) != $daili_info['password']){
            $this->error('密码错误！');
        }
        if(md5($data['password']) == $daili_info['password'] && $data['username'] == $daili_info['dl_phone']){
            session('index_daili', $daili_info);
            $this->success("登录成功！",url('user/index'));
        }
    }
    
    /**
     * 微信登录
     */
    public function wxLogin(){
        $wchat = new \wechat\WechatOauth();
        $code = request()->param('code',"");
        $user = $wchat->getUserAccessUserInfo($code);
         
        $daili_info = Db::name('daili')->where("openid = '{$user['openid']}'")->find();
        if($daili_info['id'] != ''){
            session('index_daili', $daili_info);
            $this->success("登录成功！",url('user/index'));
        }else{
            $this->success("您还没有绑定微信！",url('index/index'));
        }
    }
}
